package com.academy.notes;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import java.io.*;

import com.academy.querystapp.R;

import android.util.Log;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;



public class MainActivity extends Activity {

	EditText txtData;
	Button btnWriteSDFile;
	Button btnReadSDFile;
	Button btnClearScreen;
	Button btnClose;
	apis s;
	
	int i=0;
	String data,path=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notes_main);
        
        txtData = (EditText)findViewById(R.id.txtData);
   s=new apis();
        
    btnWriteSDFile = (Button)findViewById(R.id.btnWriteSDFile);
    btnWriteSDFile.setOnClickListener(new OnClickListener()
    {
    	public void onClick(View v)
    	{
    		try
    		{
    			
    			i++;
    			File myFile = new File("/sdcard/mysdfile"+i+".txt");  			    			
    			
    			if(myFile.exists())
				{
    				 FileReader fr = new FileReader(myFile);  
    	                BufferedReader br = new BufferedReader(fr);  
    	                data = br.readLine();
				}
		else
		{
    			
    			myFile.createNewFile();
    			FileOutputStream fOut = new FileOutputStream(myFile);
    			OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut);
    			myOutWriter.append(txtData.getText());
    			myOutWriter.close();
    			fOut.close();
    			Toast.makeText(getBaseContext(), "Done writing SD 'mysdfile.txt'", Toast.LENGTH_SHORT).show();
    			}
    			
    		}
    		catch(Exception e)
    		{
    			Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
    			
    		}
    		
    	}
    });
    
    btnReadSDFile = (Button)findViewById(R.id.btnReadSDFile);
    btnReadSDFile.setOnClickListener(new OnClickListener()
    {
    	public void onClick(View v)
    	{
    		//txtData.setText(" ");
    		
    		Intent intent = new Intent(getApplicationContext(),FileChooser.class);
    		
    		startActivityForResult(intent, 0);
    		
    		//txtData.append(s.getpath());
    		//Log.e("sfasf",""+s.getpath());
    		
    		//txtData.append("caddd");
    		
    		
    	}
    	});
    
    
    btnClearScreen = (Button)findViewById(R.id.btnClearSDFile);
    btnClearScreen.setOnClickListener(new OnClickListener()
    {
    	public void onClick(View v)
    	{
    		txtData.setText("");
    	}
    });
    
    btnClose =(Button)findViewById(R.id.btnClose);
    btnClose.setOnClickListener(new OnClickListener(){
    	public void onClick(View v)
    	{
    		finish();
    	}
    });
    
    //Toast.makeText(getBaseContext(), getpath, duration)
    
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // See which child activity is calling us back.
    	if (requestCode ==0)
    	{
    		if(resultCode == RESULT_OK)
    		{
	    	
    		Log.e("path is ",""+data );
    		txtData.append(data.getData().toString());
    		
    		}
    	}
} 
    
}